package com.ezjie.ielts.module_word;

import java.util.Random;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.application.YsApplication;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.util.PreferencesUtil;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.view.AutofitTextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

/**
 * 复习管理类， 控制复习界面和 详情界面的展示和消失
 *
 */
@ContentView(R.layout.layout_review_study_manager)
public class ReviewStudyManagerActivity extends BaseFragmentActivity {
	public static final int SHOW_WORD = 0;
	public static final int SHOW_DETAIL = 1;
	private int currentIndex;

	@ViewInject(R.id.navi_back_btn)
	private ImageView navi_back_btn;// 关闭
	@ViewInject(R.id.navi_title_text)
	private TextView navi_title_text;// 标题
	@ViewInject(R.id.pb_progress)
	private ProgressBar pb_progress;
	@ViewInject(R.id.tv_progress)
	private AutofitTextView tv_progress;// 已通过的进度
	@ViewInject(R.id.review_manager_ll)
	private LinearLayout review_manager_ll;

	private int[] imgs;
	private static int size;

	private static Random rand = new Random(47);
	/**
	 * 用于对Fragment进行管理
	 */
	private FragmentManager fragmentManager;

	private ReviewSummaryFragment reviewSummaryFragment;
	private WordParaphraseFragment wordParaphraseFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		ViewUtils.inject(this);

		fragmentManager = getSupportFragmentManager();
		removeAllFragment();
		imgs = YsApplication.getImgs();
		size = imgs.length;

		init();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_STUDYMANAGER);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_STUDYMANAGER);
	}

	private void init() {
		navi_back_btn.setOnClickListener(this);
		navi_title_text.setText(R.string.review_title);

		int img = rand.nextInt(size);
		PreferencesUtil.putInt(this, "review_bg", img);
		review_manager_ll.setBackgroundResource(imgs[img]);
		showView(SHOW_WORD);
	}

	public void showProgress(int total, int passed) {
		pb_progress.setMax(total);
		pb_progress.setProgress(passed);
		tv_progress.setText(passed + "/" + total);
	}

	/**
	 * 展示单词 还是详情
	 * 
	 * @param flag
	 */
	public void showView(int flag) {
		this.currentIndex = flag;
		// 开启一个Fragment事务
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		// 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
		hideFragments(transaction);
		switch (flag) {
		case SHOW_WORD:
			if (reviewSummaryFragment == null) {
				// 如果courseFragment为空，则创建一个并添加到界面上
				reviewSummaryFragment = new ReviewSummaryFragment();
				transaction.add(R.id.content, reviewSummaryFragment);
			} else {
				// 如果courseFragment不为空，则直接将它显示出来
				transaction.setCustomAnimations(R.anim.open_from_right, R.anim.open_to_left);
				transaction.show(reviewSummaryFragment);
			}
			break;
		case SHOW_DETAIL:
			// if (wordParaphraseFragment == null) {
			// 如果courseFragment为空，则创建一个并添加到界面上
			wordParaphraseFragment = new WordParaphraseFragment();
			transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
			transaction.add(R.id.content, wordParaphraseFragment);
			// } else {
			// // 如果courseFragment不为空，则直接将它显示出来
			// transaction.setCustomAnimations(android.R.anim.fade_in,
			// android.R.anim.fade_out);
			// transaction.show(wordParaphraseFragment);
			// }
			break;

		default:
			break;
		}
		transaction.commit();
	}

	private void removeAllFragment() {
		FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
		if (reviewSummaryFragment != null) {
			beginTransaction.remove(reviewSummaryFragment);
		}
		if (wordParaphraseFragment != null) {
			beginTransaction.remove(wordParaphraseFragment);
		}
		beginTransaction.commit();
	}

	/**
	 * 将所有的Fragment都置为隐藏状态。
	 * 
	 * @param transaction
	 *            用于对Fragment执行操作的事务
	 */
	private void hideFragments(FragmentTransaction transaction) {

		if (reviewSummaryFragment != null) {
			transaction.hide(reviewSummaryFragment);
		}
		if (wordParaphraseFragment != null) {
			transaction.hide(wordParaphraseFragment);
		}
	}

	@Override
	public void onBackPressed() {
		if (currentIndex == SHOW_WORD) {
			if (reviewSummaryFragment != null) {
				reviewSummaryFragment.onBackPressed();
			}
		} else if (currentIndex == SHOW_DETAIL) {
			if (wordParaphraseFragment != null) {
				wordParaphraseFragment.onBackPressed();
			}
		}
	}

	@Override
	public void onClickEvent(View view) {
		if (view.getId() == R.id.navi_back_btn) {
			onBackPressed();
		}
	}

}
